var defaultSupply = {
  kind_id: 0,
  name: "全国",
  level: 1,
  upid: 0
}

export default {
  state: {
    supply: {
      kind: {},
      units: {},
      province: {},
      city: {},
      area: {},
      town: {},
      sale_pre: {}
    },
    demand: {
      kind: {},
      units: {},
      province: {},
      city: {},
      area: {},
      town: {},
      sale_pre: {}
    },
    lastPostId: null, //上一个发布供应或者求购的id
    lastPostType: "", //supply代表供应，demand代表求购
    supplements: [] //快速选择标签
  },
  getters: {
    supplyDistricts: state => {
      let districts = []
      districts.push(state.supply.province)
      districts.push(state.supply.city)
      districts.push(state.supply.area)
      districts.push(state.supply.town)
      return districts
    },
    supplyDistricts: state => {
      let districts = []
      districts.push(state.demand.province)
      districts.push(state.demand.city)
      districts.push(state.demand.area)
      districts.push(state.demand.town)
      return districts
    }
  },
  mutations: {
    resetFormData(state, type = 0) {
      state.supply.units = {}
      state.supply.sale_pre = {}
      if (type === 1 || type === 0) {
        
        state.supply.province = {}
        state.supply.city = {}
        state.supply.area = {}
        state.supply.town = {}
      }
      if (type === 0) {
        state.supply.kind = {}
      }
    },
    setFormData(state, payload) {
      state.supply.kind.id = payload.kind_id
      state.supply.sale_pre = payload.sale_pre
      state.supply.province = {
        id: payload.province,
        name: payload.province_name
      }
      state.supply.city = {
        id: payload.city,
        name: payload.city_name
      }
      state.supply.area = {
        id: payload.area,
        name: payload.area_name
      }
      state.supply.town = {
        id: payload.town,
        name: payload.town_name
      }
    },
    changeKind(state, kind) {
      state.supply.kind = kind
    },
    putSupplyUnits(state, units) {
      state.supply.units = units
    },
    setSupplyProvince(state, province) {
      state.supply.province = province
      state.supply.city = {}
      state.supply.town = {}
      state.supply.area = {}
    },
    setSupplyCity(state, city) {
      state.supply.city = city
      state.supply.town = {}
      state.supply.area = {}
    },
    setSupplyArea(state, area) {
      state.supply.area = area
      state.supply.town = {}
    },
    setSupplyTown(state, town) {
      state.supply.town = town
    },
    setSalePre(state, { year, month, day }) {
      state.supply.sale_pre = { year, month, day }
    },
    //demands
    resetDemandFormData(state, type = 0) {
      state.demand.units = {}
      if (type === 1 || type === 0) {
        
        state.demand.province = {}
        state.demand.city = {}
        state.demand.area = {}
        state.demand.town = {}
      }
      if (type === 0) {
        state.demand.kind = {}
      }
    },
    setDemandFormData(state, payload) {
      state.demand.kind.id = payload.kind_id
      state.demand.province = {
        id: payload.province,
        name: payload.province_name
      }
      state.demand.city = {
        id: payload.city,
        name: payload.city_name
      }
      state.demand.area = {
        id: payload.area,
        name: payload.area_name
      }
      state.demand.town = {
        id: payload.town,
        name: payload.town_name
      }
    },
    changeDemandKind(state, kind) {
      state.demand.kind = kind
    },
    setDemandProvince(state, province) {
      state.demand.province = province
      state.demand.city = {}
      state.demand.town = {}
      state.demand.area = {}
    },
    setDemandCity(state, city) {
      state.demand.city = city
      state.demand.town = {}
      state.demand.area = {}
    },
    setDemandArea(state, area) {
      state.demand.area = area
      state.demand.town = {}
    },
    setDemandTown(state, town) {
      state.demand.town = town
    },


    setSupplements(state, supplements) {
      state.supplements = supplements
    },
    
    setLastPostId(state, { id, type }) {
      state.lastPostId = id
      state.lastPostType = type
    },

    
  },
  actions: {
    //获取补充说明标签
    async getSupplements({ commit, state }, kid) {
      if (state.supplements.length !== 0) {
        return state.supplements
      }
      const { data } = await axios.get("/api/trade/common/getDescTagList")
      commit("setSupplements", data.resp_data)
      return data.resp_data
    }
  }
}
